package top.littlefogcat.shuzihuarongdao.suanfa;

import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Stack;

public class demo {
    private TextView bd;
    String string = "";
    //    StringBuffer pp = new StringBuffer();
    private ArrayList<Integer> tipsOfNext = new ArrayList<>();

    public int suanfa() {

        String sc = "";
        int endstep = 0;
        Solution solution = new Solution();
//        HashMap<Integer, String> data = new HashMap<>();

        while (!solution.open.isEmpty()) {
            //将OPEN表按照f(n)递增排序
            Collections.sort(solution.open, new Comparator<Node>() {
                @Override
                public int compare(Node o1, Node o2) {
                    return o1.getF() - o2.getF();
                }
            });

            //选择f(n)最小的结点
            Node firstNode = solution.open.get(0);

            if (solution.isTarget(firstNode)) {
                //达到目标状态
                Stack<Node> stack = new Stack<>();
                Node node = firstNode;
                while (node != null) {
                    //回溯结果
                    stack.push(node);
                    node = node.getParent();
                }
                //输出结果
                int step = 0;
                while (!stack.isEmpty()) {

                    System.out.println("step:" + step++);

                    tipsOfNext.addAll(solution.printMatrix(stack.pop().getMatrix()));
//                    pp.append(string);

                }

//                sc = pp.toString();
                endstep = step - 1;
//                data.put(step - 1, pp.toString());

//                System.out.println("最优步数是" + endstep);
                break;
            } else {
                //将结点n移出open表,加入closed表
                solution.closed.add(firstNode);
                solution.open.remove(0);
                //遍历结点n的邻近结点
                solution.updateOpen(firstNode);
            }
        }
        return endstep;

    }

    public ArrayList<Integer> getTipsOfNext() {
        return tipsOfNext;
    }
}
